Repository navigation
Integrate upstream CPU kernel fixes (reductions, binary ops, conv, FP8, fp16 masks, float64 math) - #48
Merged
Conversation
Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit 0840c42)
(cherry picked from commit 1bed2e2)
Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit 8c56dd8)
(cherry picked from commit 017e1c3)
Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit 53fbe56)
(cherry picked from commit 0d8236f)
(cherry picked from commit 8d5bdbc)
Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit af9a592)
…explore#4756) Co-authored-by: Cheng <git@zcbenz.com> (cherry picked from commit 7d2d94a)
(cherry picked from commit a8f66b5)
rcfa
added a commit
to rcfa/mlx
that referenced
this pull request
Nov 8, 2026
The general reduction's cascade loop computed the input offset and the element count in int, so a reduction over 2^31 or more elements wrapped. Apple's 64-bit reduce offsets (in osaurus-ai#48) do the same for the other loops.
rcfa
added a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 22, 2026
Conflicts, all where main's CPU fixes meet this PR's Highway code: - cpu/reduce.cpp: strided_reduce (osaurus-ai#34's cascade) is common to both builds; the Highway contiguous_reduce sums in blocks of sum_block, as osaurus-ai#34 does. The non-Highway code is main's. - cpu/softmax.cpp: the Highway row kernel sums in blocks of sum_block, as osaurus-ai#34 does. The non-Highway code is main's. - cpu/binary.h: main's size_t loop (osaurus-ai#48) under this PR's guard. - cpu/simd/base_simd.h, tests/blas_tests.cpp, tests/ops_tests.cpp: both sides kept. Check: with MLX_USE_HIGHWAY_KERNELS undefined (unifdef), every source file reduces to main's version, except one comment in cpu/quantized.cpp that this PR already had. The tests are the union of main's and this PR's.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Branch
apple/cpu-kernels, 10 commits onorigin/main3b39b5e.Bring in ten bug fixes from ml-explore/mlx main that the fork does not have yet. The code is Apple's and its
contributors': each commit is a
git cherry-pick -xof the upstream commit, with the upstream author kept andthe
(cherry picked from commit …)line recording provenance. Nothing of ours is in these commits.Why we push them: osaurus and vmlx-swift ship from this fork, and these are wrong-result or memory-safety bugs on
the CPU backend, which vmlx-swift also runs on Linux. Each one is still present on
origin/main: the fork has notchanged these functions since the merge base 64ea011, and each upstream change applies to them unchanged.
Included, in upstream order:
nd_loopcallback inint, so inputs past 2^31 elements read the wrong rows or out of bounds; the Metal row reductions now pick the 64-bit kernel whendata_size()(not onlysize()) passes 2^31.-0+0jcounted as true; adds an explicitcomplex64_t::operator bool, a complex64 and/or reduce on Metal, and the CUDA cast (&&→||,True→1+0j).!masks were built from*(uint16_t*)&out, which reads lane 0 and broadcasts it to every lane; now loads all lanes (arm64 CPU, macOS and Linux)..so; now builds under a unique name and renames into place.breakafter thebool_case inbinary_int_op_cpu: bool inputs fell through and also ran the uint8 kernel.binary_op_dispatch_dimstook the element count asint(truncated on large arrays).to_fp8turned NaN into the saturated max (0x7E) instead of NaN (0x7F), and an E8M0 scale of 0xFF decoded to +inf instead of NaN (CPU and Metalfp8.h).exp,sin,cosanderfon the CPU were computed through the float32 SIMD path.Not included here: the other core/CPU/io fixes are in
apple/array-views,apple/transforms-opsandapple/io.Left out of the whole intake: ml-explore#4694 (shift fix, already our #41) and ml-explore#4634 (foreign buffer ownership: it changes
allocator::mallocto returnallocator::Dataand removesarray(Buffer, …, Deleter)andallocator::free, whichthe fork's mmap safetensors loader uses; listed for review by the fork's maintainer).
Conflict resolutions
Library code applied without conflicts. Two test files:
python/tests/test_ops.py: its only hunk deletes theLOW_MEMORYskip ontest_large_binary; the hunk'scontext is
test_unique, which the fork lacks. Kept the fork's file, sotest_large_binarykeeps its skip.Apple's new
test_large_offsets(test_reduce.py, 2^31 + 2^16 elements) went in unchanged.python/tests/test_conv.py: its only hunk changes the skip ontest_input_dilation_grad, a test that comeswith Fix negative padding with input_dilation > 1 in conv_general ml-explore/mlx#4688 in
apple/transforms-ops. Kept the fork's file; itstest_double.pycases went in.Overlaps with open PRs
mlx/backend/cpu/reduce.cpp: Sum long float rows in blocks on the CPU #34 is written against theintsizes and offsets that Fix reduction over large arrays ml-explore/mlx#4549 widens. Sum long float rows in blocks on the CPU #34 should be rebased onto this branch withint64_tsizes, offsets andnd_loopcallbacks, or Fix reduction over large arrays ml-explore/mlx#4549's 2^31 bug comes back through Sum long float rows in blocks on the CPU #34's new loops.mlx/backend/cpu/binary.h, one line: Fix cpu binary_op_dispatch_dims on large data ml-explore/mlx#4648 makes the loop indexsize_twhereCPU backend: adopt ml-explore/mlx#3019's Highway kernels on Linux x86-64 #10 wraps the same loop in
#if !defined(MLX_USE_HIGHWAY_KERNELS). Resolution: keep the#ifand thesize_t.files.
fix/float-to-int-saturate) conflicts incpu/copy.cppwith Fix complex to bool ml-explore/mlx#4593's
copy_vectorhunk, which that work makes redundant; take T-276's version there. Fix complex to bool ml-explore/mlx#4593's all/anyhunks are still needed.
apple/metal-kernels(test_conv.py, test_reduce.py): both sides add tests at the sameplace; keep both.
Validation
Compared with
main(3b39b5e), built and run the same way:Linux arm64 (Debian in a VM on an M4 Max), C++ suite on the CPU, GCC 12 Debug and clang 21 Debug with UBSan:
the same failing cases as
mainand no UBSan report thatmainlacks. OnmainGCC fails onlytest one-bit affine endpoints CPU GPU and small-K dispatch(needs a GPU), and clang the same plus 16compile/fusioncases whose CPUJIT fails in that VM.
macOS (M4 Max, Apple clang 21), C++ suite, Release, on the CPU and on Metal.
Python (macOS, Python 3.14, the extension in Release with Metal), on the GPU and with
DEVICE=cpu: the test filesthis PR touches, with this branch and with
main. Each upstream test method this PR adds or changes also ran aloneagainst
main's build, with this branch's version of its file.Linux: 285 cases (
main284).macOS: 321 cases (
main320), all pass on the CPU and on Metal (mainfailstest concurrent allocator statisticson Metal in that run; it fails intermittently).
Python,
test_conv,test_double,test_ops,test_quantized,test_reduce: the failing subtests ofmainareunchanged (
test_qmm_mid_mandtest_qmm_splitk_precisionprecision thresholds on the GPU; tests that need a GPUstream under
DEVICE=cpu), and the new methods pass on both devices. The upstream methods againstmain(GPU / CPU):
test_large_offsets: fail / crash (SIGBUS)test_complex_bool_cast,test_and_or_negative_zero: fail / failtest_dynamic_slicing: fail / failtest_bitwise_ops: ok / fail (the fix is in the CPU kernel)test_grouped_conv_padding_stays_in_output: crash / crash (the test uses the CPU stream)test_to_from_fp8: fail / fail;test_mxfp8_quantize_dequantize: ok / ok, so it guards against aregression here
test_unary_ops_keep_double_precision: fail / fail🤖 Generated with Claude Code